import java.io.BufferedReader;
import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileReader;
import java.io.IOException;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

public class AshiStateFetcher {

	private static Logger logger = LoggerFactory.getLogger(AshiStateFetcher.class
			.getName());

	/**
	 * Service testing method
	 * 
	 * @param args
	 * @throws InvalidZipException
	 */
	public static void main(String[] args) {
		logger.debug("Starting..");
		
		// For DEBUG
		/*
		String url = "http://www.ashi.org/homeinspectors/Alabama/Tuscaloosa/17408JB.aspx";
		HtmlPageFetcher.fetchToDisk(url, 0);
		System.exit(0);
		*/
		
		int count = 0;
		File file = new File("/mads/hobby/it/data/stateLinks.txt");
		BufferedReader reader = null;
		try {
			reader = new BufferedReader(new FileReader(file));
			String uri = reader.readLine();
			while (uri != null) {
				UriFetcher.fetchToDisk(uri, count);
				count++;
				// delay();
				uri = reader.readLine();
			}
		} catch (FileNotFoundException e) {
			logger.error("Error", e);
		} catch (IOException e) {
			logger.error("Error", e);
		} finally {
			try {
				reader.close();
			} catch (IOException e) {
				logger.error("Error", e);
			}
		}
	}

	private static void delay() {
		logger.debug("delaying 10s");
		try {
			Thread.sleep(10000);
		} catch (InterruptedException e) {
			e.printStackTrace();
		}
	}

}
